package com.teufandsub.socialproximity.gui;


import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;

import com.google.android.gcm.GCMRegistrar;
import com.teufandsub.socialproximity.R;
import com.teufandsub.socialproximity.controller.Constants;
import com.teufandsub.socialproximity.controller.Controller;
import com.teufandsub.socialproximity.services.BackgroundService;

/**
 * @author Bastien Mathias <bastienmathias@gmail.com>
 * @author Antoine Teuf <antoine.teuf@gmail.com>
 * @version 1.0
 * @since 2013-05-22
 * 
 * The Index activity, where everything is started or loading before the user can use the application.
 */

public class IndexActivity extends Activity {

	private Controller controller;
	protected void onCreate(android.os.Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_index);
		controller = Controller.getInstance();
		
		
		//GCM Stuff:
		GCMRegistrar.checkDevice(this);
		GCMRegistrar.checkManifest(this);
		
		controller.setRegId(GCMRegistrar.getRegistrationId(this));
		if (controller.getRegId().equals("")) {
		  GCMRegistrar.register(this, Constants.SENDER_ID);
		} else {
		  Log.v(Constants.APP_NAME, "Already registered");
		}
		startService(new Intent(this, BackgroundService.class));
					
		if(!controller.checkSession(this,HomeActivity.class)){
			new Handler().postDelayed(new Runnable() {
				//2.5 seconds of waiting before redirect 
			    @Override
			    public void run() {
			        Intent startActivity = new Intent(IndexActivity.this, LoginActivity.class);
			        startActivity(startActivity);
			        finish();
			    }
			}, 2500);
		}
	};
}
